home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_055 / csh / rawconsole.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  7KB  |  274 lines

  1. /*
  2.  * RawConsole.c
  3.  *
  4.  * Shell 2.05M  20-Jan-87
  5.  * console handling, command line editing support for Shell
  6.  * using new console packets from 1.2.
  7.  * Written by Steve Drew. (c) 14-Oct-86.
  8.  * 16-Dec-86 Slight mods to rawgets() for Disktrashing.
  9.  *
  10.  */
  11. #include "shell.h" 
  12.  
  13. #define ACTION_SCREEN_MODE 994L
  14. #define DOSTRUE         -1L
  15.  
  16. struct MsgPort        *replyport, *conid;
  17. struct StandardPacket *packet;
  18.  
  19. initconsole()
  20. {
  21.     struct Process *myprocess;
  22.   
  23.     if (IsInteractive(Input())) {
  24.         myprocess = (struct Process *) FindTask(NULL);      
  25.         replyport = (struct MsgPort *) CreatePort(NULL,NULL);
  26.         if(!replyport) exit(10);
  27.         packet = (struct StandardPacket *) 
  28.             AllocMem((long)sizeof(*packet),MEMF_PUBLIC | MEMF_CLEAR);
  29.         conid = (struct MsgPort *) myprocess->pr_ConsoleTask; /* get console handler */
  30.         printf("\23312{");
  31.         set_var (LEVEL_SET, "_insert", "1");
  32.     }
  33. }
  34.  
  35. void
  36. cleanupconsole()
  37. {
  38.  
  39.     if (IsInteractive(Input())) {
  40.        FreeMem(packet,(long)sizeof(*packet)); 
  41.        DeletePort(replyport); 
  42.     }
  43. }
  44.  
  45. void 
  46. setraw(onoff)
  47. int onoff; 
  48. {
  49.  
  50.  packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt); /* link packet- */
  51.  packet->sp_Pkt.dp_Link         = &(packet->sp_Msg);        /* to message    */
  52.  packet->sp_Pkt.dp_Port         = replyport;         /* set-up reply port   */
  53.  packet->sp_Pkt.dp_Type = ACTION_SCREEN_MODE;        /* function */
  54.  
  55.  if (onoff) { 
  56.      packet->sp_Pkt.dp_Arg1 = DOSTRUE; 
  57.      fflush(stdout);             /* I had previously set stdout to buf'rd */
  58.      stdout->_buflen = 1;        /* but for raw mode we need single char */
  59.      }
  60.  else {
  61.      packet->sp_Pkt.dp_Arg1 = FALSE;
  62.      stdout->_buflen = STDBUF;            /* set back to buffr'd */
  63.  }
  64.  PutMsg(conid,packet);  /* send packet */
  65.  WaitPort(replyport); /* wait for packet to come back */
  66.  GetMsg(replyport);   /* pull message */
  67.  
  68. }
  69.  
  70.  
  71. char *
  72. rawgets(line,prompt)
  73. char *line, *prompt;
  74. {
  75.     char *get_var();
  76.     char *gets();
  77.     register int n, pl;
  78.     register int max, i;
  79.     unsigned char c1,c2,c3;
  80.     char fkeys[5];
  81.     char *s;
  82.     int fkey;
  83.     int insert = 1;
  84.     char rep[14];
  85.     static int width;
  86.     int recall = -1;
  87.     struct HIST *hist;
  88.  
  89.     if (!IsInteractive(Input())) return(gets(line));
  90.     if (WaitForChar((long)Input(), 100L)) {   /* don't switch to 1L ...*/
  91.     /*  printf("%s",prompt); */               /* else causes read err's  */
  92.     gets(line);
  93.     return(line);
  94.     } 
  95.     setraw(1);
  96.     printf("%s",prompt);
  97.     max = pl = i = strlen(prompt);
  98.     strcpy(line,prompt);
  99.     if (!width) width = 77;
  100.     if (s = get_var (LEVEL_SET, "_insert"))
  101.         insert = atoi(s) ? 1 : 0;
  102.  
  103.     while((c1 = getchar()) != 255) {
  104.     if (c1 < 156) switch(c1) {    
  105.         case 155:
  106.          c2 = getchar();
  107.          switch(c2) {
  108.              case 'A':            /* up arrow   */
  109.             n = ++recall;
  110.              case 'B':            /* down arrow */
  111.             line[pl] = '\0';
  112.             if (recall >= 0 || c2 == 'A') {
  113.                 if (c2 == 'B') n = --recall;
  114.                 if (recall >= 0) {
  115.                     for(hist = H_head; hist && n--; 
  116.                         hist = hist->next);
  117.                     if (hist) strcpy(&line[pl],hist->line);
  118.                     else recall = H_len;
  119.                 }
  120.             }
  121.             if (i != pl) 
  122.                 printf("\233%dD",i);
  123.             printf("\015\233J%s",line);
  124.             i = max = strlen(line);
  125.             break;
  126.              case 'C':            /* right arrow*/
  127.             if (i < max) {
  128.                 i++;
  129.                 printf("\233C");
  130.             }
  131.             break;
  132.              case 'D':            /* left arrow */
  133.             if (i > pl) {
  134.                 i--;
  135.                 printf("\233D");
  136.             }
  137.             break;
  138.             case 'T':            /* shift up   */
  139.             case 'S':            /* shift down */
  140.             break;
  141.             case ' ':            /* shift -> <-*/
  142.             c3 = getchar();
  143.             break;
  144.             default:
  145.             c3 = getchar();
  146.             if (c3 == '~') {
  147.                 fkey = c2;
  148.                             fkeys[0] = 'f';
  149.                 if (c2 == 63) {
  150.                 strcpy(&line[pl],"help");
  151.                 goto done;
  152.                 }
  153.             }
  154.             else if (getchar() != '~') { /* window was resized */
  155.                     while(getchar() != '|');
  156.                                printf("\2330 q"); /* get window bounds */
  157.                         n = 0;
  158.                     while((rep[n] = getchar()) != 'r' && n++ < 14 );
  159.                     width = (rep[n-3] - 48) * 10 + rep[n-2] - 48;
  160.                     rep[n-1] = '\0';
  161.                     set_var (LEVEL_SET, "_width", &rep[n-3]);
  162.                             break;
  163.                 }
  164.                 else {
  165.                             fkey = c3;
  166.                             fkeys[0] = 'F';
  167.                         }
  168.             sprintf(fkeys+1,"%d",fkey - 47);
  169.             if (!(s = get_var(LEVEL_SET, fkeys))) break;
  170.             strcpy(&line[pl], s);
  171.             printf("%s",&line[pl]);
  172.             goto done;
  173.             break;
  174.             } 
  175.         break;
  176.         case 8:
  177.         if (i > pl) {
  178.             i--;
  179.             printf("\010");
  180.         }
  181.         else break;
  182.         case 127:
  183.         if (i < max) {
  184.             int j,t,l = 0;
  185.             movmem(&line[i+1],&line[i],max-i);
  186.             --max;
  187.             printf("\233P");
  188.             j = width - i % width - 1;     /* amount to end     */
  189.             t = max/width - i/width;     /* no of lines       */
  190.             for(n = 0; n < t; n++) {
  191.             l += j;             /* no. of char moved */
  192.             if (j) printf("\233%dC",j); /* goto eol       */
  193.             printf("%c\233P",line[width*(i/width+n+1)-1]);
  194.             j = width-1;
  195.             }
  196.             if (t)
  197.             printf("\233%dD",l+t);   /* get back */
  198.         }
  199.         break;
  200.         case 18:
  201.         n = i/width;
  202.         if (n) printf("\233%dF",n);
  203.         printf("\015\233J%s",line);
  204.         i = max;
  205.         break;
  206.         case 27:
  207.         case 10:
  208.         break;
  209.         case 1:
  210.         insert ^= 1;
  211.         break;
  212.         case 21:
  213.         case 24:
  214.         case 26:
  215.         if (i > pl)
  216.             printf("\233%dD",i-pl);
  217.         i = pl;
  218.         if (c1 == 26) break;
  219.         printf("\233J");
  220.         max = i;
  221.         line[i] = '\0';
  222.         break;
  223.         case 11:        /* ^K */
  224.             printf("\233J");
  225.             max = i;
  226.             line[i] = '\0';
  227.             break; 
  228.             case 28:        /* ^\ */
  229.                 setraw(0);
  230.                 return(NULL);
  231.         case 5:
  232.         printf("\233%dC",max - i);
  233.         i = max;
  234.         break;
  235.         case 13:
  236.         line[max] = '\0';
  237. done:        printf("\233%dC\n",max - i);
  238.  
  239.         setraw(0);
  240.         strcpy(line, &line[pl]);
  241.         return(line);
  242.         default:
  243.         if (c1 == 9) c1 = 32;
  244.         if (c1 > 31 & i < 256) {
  245.             if (i < max && insert) {
  246.             int j,t,l = 0;
  247.             movmem(&line[i], &line[i+1], max - i);
  248.             printf("\233@%c",c1);
  249.             t = max/width - i/width;
  250.             j = width - i % width - 1;
  251.             for(n = 0; n < t; n++) {
  252.                 l += j;
  253.                 if (j) printf("\233%dC",j);
  254.                 printf("\233@%c",line[width*(i/width+n+1)]);
  255.                 j = width-1;
  256.             }
  257.             if (t) printf("\233%dD",l + t);
  258.             ++max;                
  259.             }
  260.             else {
  261.             if (i == pl && max == i) printf("\015%s",line);
  262.             putchar(c1);
  263.             }
  264.             line[i++] = c1;
  265.             if (max < i) max = i;
  266.             line[max] = '\0';
  267.         }
  268.     }
  269.     }
  270.     setraw(0);
  271.     return(NULL);
  272. }
  273.  
  274.